package com.amkai.mobile.client.appointmentdetails;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.amkai.mobile.bean.scheduler.appointment.MobileAppointmentDetails;
import com.amkai.mobile.bean.scheduler.appointment.MobileSurgeryProcedureUsage;
import com.amkai.mobile.client.R;
import com.amkai.mobile.client.common.AEMobileConstants;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJob;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJobResult;
import com.amkai.mobile.utils.manager.job.MobileServiceManager;

/**
 * 
 * @author lkovari
 *
 */
public class AppointmentTabSC extends Activity {
	private TextView timeRangeTextView = null;
	private TextView apptTypeValueTextView = null;
	private TextView apptStatusTextView = null;
	private TextView apptTypeColorTextView = null;
	private TextView targetLabelTextView = null;
	private TextView targetValueTextView = null;
	private TextView surgeonTextView = null;
	private TextView anestTextView = null;
	private TextView crnaTextView = null;
	private ListView procsListView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.appointmenttabsc);

        // capture ids
        Bundle bundle = getIntent().getExtras();
        Long beId = bundle.getLong(AEMobileConstants.BUNDLE_CONTENT_TAG_DEPARTMENTID);
        Long passId = bundle.getLong(AEMobileConstants.BUNDLE_CONTENT_TAG_PASSPORTID);
        Long apptId = bundle.getLong(AEMobileConstants.BUNDLE_CONTENT_TAG_APPOINTMENTID);
        
        // download appointmentTetails
		try {
			MobileSchedulerJobResult schedulerResult = MobileServiceManager.processMobileJob(AppointmentTabSC.this, MobileSchedulerJob.appointmentDetailsById(apptId));
			if (schedulerResult.getException() == null) {
				MobileAppointmentDetails mobileAppointmentDetails = schedulerResult.getMobileAppointmentDetails();
				showAppointmentDetails(mobileAppointmentDetails);
			}
			else {
				Toast msg = Toast.makeText(AppointmentTabSC.this, "Download appointment data failed!", Toast.LENGTH_SHORT);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void showAppointmentDetails(MobileAppointmentDetails appointmentDetails) {
        timeRangeTextView = (TextView) findViewById(R.id.timeValueTextView);
        String cleanupText = "";
        if (appointmentDetails.cleanup > 0)
        	cleanupText = " clnup:" + appointmentDetails.cleanup;
        timeRangeTextView.setText(appointmentDetails.beginTime + " - " + appointmentDetails.endTime + cleanupText);

    	apptStatusTextView = (TextView) findViewById(R.id.statusValueTextView);
    	apptStatusTextView.setText(appointmentDetails.status);
        
        apptTypeValueTextView = (TextView) findViewById(R.id.apptTypeValueTextView);
        apptTypeValueTextView.setText(appointmentDetails.mobileAppointmentType.name + " ");
        apptTypeValueTextView.setTextColor(appointmentDetails.mobileAppointmentType.color);
        //apptTypeColorTextView = (TextView) findViewById(R.id.apptTypeColorTextView);
        //apptTypeColorTextView.setBackgroundColor(appointmentDetails.mobileAppointmentType.color);
        
        targetLabelTextView = (TextView) findViewById(R.id.targetLabelTextView);
        targetValueTextView = (TextView) findViewById(R.id.targetValueTextView);
        surgeonTextView = (TextView) findViewById(R.id.surgValueTextView);
        anestTextView = (TextView) findViewById(R.id.anesValueTextView);
        crnaTextView = (TextView) findViewById(R.id.crnaValueTextView);
        if (appointmentDetails.isSCorSH) {
        	targetLabelTextView.setText("Room ");
        	targetValueTextView.setText(appointmentDetails.mobileSchedulerTarget.name);
            surgeonTextView.setText(appointmentDetails.mobileSurgeon.name);
//FIXME Need use instead of textview a table or list            
            if (appointmentDetails.mobileAnest != null)
            	anestTextView.setText(appointmentDetails.mobileAnest.name);
            if (appointmentDetails.mobileCRNA != null)
            	crnaTextView.setText(appointmentDetails.mobileCRNA.name);
            
            if (appointmentDetails.mobileSurgeryProcedureUsages != null) {
                procsListView = (ListView) findViewById(R.id.procsListView);
                String[] listItems = new String[appointmentDetails.mobileSurgeryProcedureUsages.size()];
                String line = "";
                int ix = 0;
            	for (MobileSurgeryProcedureUsage mspu : appointmentDetails.mobileSurgeryProcedureUsages) {
            		line = "PROC " + mspu.mobileProcedure.shortName;
            		if (mspu.mobileProcedureType != null) {
                		line += " PROCTYPE " + mspu.mobileProcedureType.shortName;
            		}
            		if (mspu.modProcDescr != null) {
                		line += " MODPROC " + mspu.modProcDescr;
            		}
            		line += " SURGEON " + mspu.mobileSurgeon.name;
            		if (mspu.lrb != null) {
                		line += " LRB " + mspu.lrb;
            		}
            		if (mspu.mobileAssistant1 != null) {
                		line += " ASSISTANT 1 " + mspu.mobileAssistant1.name;
            		}
            		if (mspu.mobileAssistant2 != null) {
                		line += " ASSISTANT 2 " + mspu.mobileAssistant2.name;
            		}
            		if (mspu.dx1 != null) {
                		line += " DX 1 " + mspu.dx1.shortName;
            		}
            		if (mspu.dx2 != null) {
                		line += " DX 2 " + mspu.dx2.shortName;
            		}
            		if (mspu.dx3 != null) {
                		line += " DX 3 " + mspu.dx3.shortName;
            		}
            		if (mspu.dx4 != null) {
                		line += " DX 4 " + mspu.dx4.shortName;
            		}
            		listItems[ix] = line;
            		ix++;
            	}
                ListAdapter listAdapter = new ArrayAdapter<String>(this, R.layout.procslistviewitem, listItems); 
                procsListView.setAdapter(listAdapter);
            }
        }
        
        
	}
}
